package com.example.snashuitraverl.demos.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.snashuitraverl.demos.common.BaseResponse;
import com.example.snashuitraverl.demos.common.ErrorCode;
import com.example.snashuitraverl.demos.common.ResultUtils;
import com.example.snashuitraverl.demos.domain.Ads;
import com.example.snashuitraverl.demos.domain.DTO.AdsDto;
import com.example.snashuitraverl.demos.domain.User;
import com.example.snashuitraverl.demos.exception.BusinessException;
import com.example.snashuitraverl.demos.service.AdsService;
import com.example.snashuitraverl.demos.service.UserService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
public class AdsController {

    @Resource
    private AdsService adsService;
    @Resource
    private UserService userService;

    @PostMapping("/addAds")
    public BaseResponse<Integer> addAds(Ads ads, HttpServletRequest request){
        int i = adsService.addAds(ads, request);
        return i==1? ResultUtils.success(i,"成功"):ResultUtils.error(ErrorCode.PARAMS_ERROR,"失败");
    }
    @PostMapping("/selectAll")
    public BaseResponse<List<Ads>> selectAll(HttpServletRequest request){
        List<Ads> ads = adsService.selectAll(request);
        return ads!=null?ResultUtils.success(ads,"成功"):ResultUtils.error(ErrorCode.PARAMS_ERROR);
    }
    @PostMapping("/selectByID")
    public BaseResponse<Ads> selectByID(int ID,HttpServletRequest request){
        Ads ads = adsService.selectByID(ID, request);
        return ads!=null?ResultUtils.success(ads,"成功"):ResultUtils.error(ErrorCode.PARAMS_ERROR);
    }

    @ApiOperation(value = "管理端的管理员的分页查询")
    @GetMapping("/selectPage")
    public BaseResponse<Page<Ads>> selectAll(AdsDto adsDto, HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        if (loginUser == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        return ResultUtils.success(adsService.selectPage(adsDto, request), "查询成功");
    }
}
